package org.devocean.groupbuying.web.frontend.displaybeans;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.devocean.groupbuying.domain.Article;
import org.devocean.groupbuying.domain.Compatibility;
import org.devocean.groupbuying.domain.Deal;
import org.devocean.groupbuying.domain.Merchant;
import org.devocean.groupbuying.domain.Plan;
import org.devocean.groupbuying.domain.Product;
import org.devocean.groupbuying.domain.Promotion;
import org.devocean.groupbuying.domain.Tags;

public class ProductBean {

	private Product product;

	private Article productArticle;

	private boolean promotions;

	private boolean deals;

	private Merchant merchant;
	
	private MerchantBean merchantBean;

	private String merchantMediaPath;

	private String productMediaPath;

	private List<PlanBean> plansBean;
	
	private List<Tags> tags;
	

	public ProductBean(Product product) {

		this.product = product;
		this.setProductArticle(this.product.getArticlesLocalization()
				.getArticles().iterator().next());
		this.merchant = product.getMerchant();
		this.merchantMediaPath = this.merchant.getArticlesLocalization()
				.getMediaLocalizations().iterator().next().getMediaFilePath();
		this.productMediaPath = this.product.getArticlesLocalization()
				.getMediaLocalizations().iterator().next().getMediaFilePath();
		Iterator<Plan> it = this.product.getPlans().iterator();
		plansBean = new ArrayList<PlanBean>();
		while (it.hasNext()) {
			Plan plan = it.next();

			PlanBean planBean = new PlanBean(plan);

			this.plansBean.add(planBean);
		}
		tags = new ArrayList<Tags>();

	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Set<Compatibility> getCompatibilities() {
		Set<Compatibility> c = new HashSet<Compatibility>();
		for (Plan plan : this.product.getPlans()) {
			for (Compatibility compatibility : plan.getCompatibilities()) {
				c.add(compatibility);
			}
		}

		return c;
	}

	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	public String getMerchantMediaPath() {
		return merchantMediaPath;
	}

	public void setMerchantMediaPath(String merchantMediaPath) {
		this.merchantMediaPath = merchantMediaPath;
	}

	public String getProductMediaPath() {
		return productMediaPath;
	}

	public void setProductMediaPath(String productMediaPath) {
		this.productMediaPath = productMediaPath;
	}

	public Article getProductArticle() {
		return productArticle;
	}

	public void setProductArticle(Article productArticle) {
		this.productArticle = productArticle;
	}

	public List<PlanBean> getPlansBean() {
		return plansBean;
	}

	public void setPlansBean(List<PlanBean> plansBean) {
		this.plansBean = plansBean;
	}

	public boolean isPromotions() {
		promotions = false;
		for (Plan plan : product.getPlans()) {
			for (Promotion promotion : plan.getPromotions()) {
				if (Calendar.getInstance().getTime()
						.before(promotion.getExpireDate())) {
					promotions = true;
				}
			}
		}
		return promotions;
	}

	public void setPromotions(boolean promotions) {
		this.promotions = promotions;
	}

	public boolean isDeals() {
		deals = false;
		for (Deal deal : product.getDeals()) {
			if (deal.getStatus().equals("pending")
					|| deal.getStatus().equals("active")) {
				this.deals = true;
			}
		}
		return deals;
	}

	public void setDeals(boolean deals) {
		this.deals = deals;
	}

	public MerchantBean getMerchantBean() {
		merchantBean = new MerchantBean(this.product.getMerchant());
		return merchantBean;
	}

	public void setMerchantBean(MerchantBean merchantBean) {
		this.merchantBean = merchantBean;
	}

	public List<Tags> getTags() {
		tags.addAll(product.getArticlesLocalization().getTags());
		return tags;
	}

	public void setTags(List<Tags> tags) {
		this.tags = tags;
	}

}
